$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/merchantorder/list',
        datatype: "json",
        colModel: [			
			// { label: 'id', name: 'id', index: 'ID', width: 50, key: true },
			// { label: '用户ID', name: 'userId', index: 'USER_ID', width: 50 },
            { label: '用户', name: 'userId', index: 'USER_ID', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/users/info/"+rowdata.userId,
                        success: function(r){
                            username = r.users.username;

                        }
                    });
                    return username;
                }
            },
			{ label: '订单编号', name: 'orderSn', index: 'ORDER_SN', width: 100 },
			{ label: '订单价格', name: 'orderAmount', index: 'ORDER_AMOUNT', width: 50 },
			// { label: '0 租借中 1 已完成 2 已撤销 3 订单超时 4 申请结束 5 丢失充电宝', name: 'orderStatus', index: 'ORDER_STATUS', width: 80 },
            { label: '订单状态', name: 'orderStatus', width: 50, formatter: function(value, options, row){
                if (value ==0) {
                    return '<span class="label label-danger">租借中</span>' ;
                }  else if (value ==1) {
                    return '<span class="label label-danger">已完成</span>' ;
                }  else if (value ==2) {
                    return '<span class="label label-danger">已撤销</span>' ;
                }
                }},
			// { label: '支付状态0 未付款 1 已付款', name: 'payStatus', index: 'PAY_STATUS', width: 80 },
			{ label: '下单时间', name: 'addTime', index: 'ADD_TIME', width: 80 }, 			
			// { label: '0 充电宝订单  1 密码线订单', name: 'status', index: 'STATUS', width: 80 },
			// { label: '商户id', name: 'merchantid', index: 'merchantid', width: 80 },
            { label: '商户', name: 'merchantid', index: 'merchantid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var name ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/merchant/info/"+rowdata.merchantid,
                        success: function(r){
                            name = r.merchant.name;

                        }
                    });
                    return name;
                }
            },
			// { label: '设备编号', name: 'devicceSn', index: 'devicce_sn', width: 80 },
			{ label: '使用时长', name: 'useduration', index: 'useDuration', width: 50 },
			{ label: '截止时间', name: 'endtime', index: 'endtime', width: 80 }, 			
			{ label: '实付金额', name: 'actualAmount', index: 'actual_amount', width: 50 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            orderSn: null,
            userId: null,
            merchantid: null,
            status: null
        },
		showList: true,
		title: null,
		shopOrder: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.shopOrder = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.shopOrder.id == null ? "sys/shoporder/save" : "sys/shoporder/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.shopOrder),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/shoporder/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/shoporder/info/"+id, function(r){
                vm.shopOrder = r.shopOrder;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            vm.q.merchantid = vm.shopOrder.merchantid;
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'status': vm.q.orderStatus,
                    'orderSn': vm.q.orderSn,
                    'userId': vm.q.userId,
                    'merchantid': vm.q.merchantid
                    // 'adminPhone': vm.q.adminPhone
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});